package com.example.serverSide.controller;

import com.example.serverSide.commom.ResultData;
import com.example.serverSide.commom.dto.DeptAccess;
import com.example.serverSide.pojo.SysDept;
import com.example.serverSide.service.MenuCheckService;
import com.example.serverSide.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/dept")
public class SysDeptController {
    @Autowired
    SysDeptService sysDeptService;
    @Autowired
    MenuCheckService menuCheckService;
    @GetMapping("/deptList")
    public ResultData getDeptList(){
        List<SysDept> sysDepts = sysDeptService.getDeptList();
        return ResultData.success(sysDepts);
    }
    @GetMapping("/getDept/{id}")
    public ResultData getDeptById(@PathVariable("id") String id) {
        SysDept sysDept = sysDeptService.getSysDeptById(id);
        return ResultData.success(sysDept);
    }
    @PostMapping("/deleteDept/{id}")
    public ResultData deleteDeptById(@PathVariable("id") String id){
        sysDeptService.deleteDeptById(id);
        menuCheckService.deleteByDeptId(id);
        return ResultData.success("删除成功!");
    }
    @PostMapping("/deleteDepts")
    public ResultData deleteDepts(@RequestBody DeptAccess[] deptAccess){
        for (DeptAccess dept: deptAccess){
            String deptId = dept.getId().toString();
            sysDeptService.deleteDeptById(deptId);
            menuCheckService.deleteByDeptId(deptId);
        }
        return ResultData.success("删除成功!");
    }
}
